package com.ljpz.web.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljpz.common.httpclient.HttpResult;
import com.ljpz.common.service.ApiService;
import com.ljpz.sso.query.bean.User;
import com.ljpz.web.bean.Cart;
import com.ljpz.web.bean.Order;
import com.ljpz.web.bean.OrderItem;
import com.ljpz.web.threadLocal.UserThreadLocal;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpDelete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * Created by liujunchen on 2017/1/11.
 */
@Service
public class CartService {
    @Autowired
    private ApiService  apiService ;
    @Value("${LJPZ_CART_URL}")
    private String LJPZ_CART_URL ;
    private   static  final ObjectMapper MAPPER  = new ObjectMapper();
    public List<Cart>queryCartList(){
        User user= UserThreadLocal.get();
        String url = LJPZ_CART_URL +"/service/api/cart/" +user.getId();
        try {

            String jsonData= this.apiService.doGet(url)  ;
            if (StringUtils.isNotEmpty(jsonData)){
                return  MAPPER.readValue(jsonData,
                        MAPPER.getTypeFactory().constructCollectionType(List.class,Cart.class));
            }
        } catch (Exception  e) {
            e.printStackTrace();
        }
        return  null ;
    }

    /**
     * 删除购物车中下单成功的商品同时删除对应COOKIE中的内容
     *
     * @param order
     */
    public   Boolean deleteCartList(Order order){
         User  user=UserThreadLocal.get() ;
         String url = LJPZ_CART_URL+"/service/api/cart/delete/"+user.getId() ;
        Boolean flag  = false  ;
        StringBuffer    itemIds = new StringBuffer() ;
        Map<String,String >  map   = new HashMap<String,String>();
        for (OrderItem orderItem  : order.getOrderItems())
        {
            itemIds.append(orderItem.getItemId().toString()) ;
            itemIds.append('|')  ;
        }
        map.put("itemIds",itemIds.toString())  ;
        try {
            //对于购物车以及对应cookie中的信息进行删除
            if(this.apiService.doDelete(url, map)==204){
                //删除成功
                flag=true ;
            }else
                flag=false ;
        } catch (Exception e) {
            e.printStackTrace();
        }
            return  flag   ;

    }


   /* @RequestMapping(method = RequestMethod.DELETE)
    public  ResponseEntity<Void>delete(ContentCategory contentCategory){
        try {
            this.contentCategoryService.deleteAll(contentCategory);
            return    ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }catch (Exception e ){
            e.printStackTrace();
        }
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }*/

}
